import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { useUserStore } from './user'
import { insertCartAPI,findNewCartListAPI,delCartAPI } from '@/apis/cart'

export const useCartStore = defineStore('cart', () => {
    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)
    //1.定义state-cartList
    const cartList = ref([])
    //2.定义action-addCart
    const addCart = async (goods) => {
        const {skuId,count} =goods
        if (isLogin) {
            //登录之后的加入购物车逻辑
            await insertCartAPI({skuId,count})
            updateNewList()


        } else {
            //添加本地购物车操作
            //已添加过count+1
            //没有添加过-直接push
            //思路：通过匹配传递的商品对象中的skuId能不能再cartList中找到，找到了就是添加过
            const item = cartList.value.find((item) => goods.skuId === item.skuId)
            if (item) {
                //找到了，原来的基础上++
                item.count++
            } else {
                //没有找到，直接push
                cartList.value.push(goods)
            }
        }


    }
    //删除购物车
    const delCart = async(skuId) => {
        if (isLogin.value) {
            //调用接口实现接口购物车中的删除功能
            await delCartAPI([skuId])
            updateNewList()
            
        }else{
        //本地删除
        //思路1：通过skuId找到对应的商品对象，然后从cartList中删除
        //思路2：通过filter方法过滤掉skuId对应的商品对象，然后重新赋值给cartList
        const idx = cartList.value.findIndex((item) => skuId === item.skuId)
        cartList.value.splice(idx, 1)
        }
    }

    //获取最新购物车列表action
    const updateNewList=async()=>{
        const res= await findNewCartListAPI()
        cartList.value = res.result
    }



    //计算属性
    //1.总的数量,所有项的count累加
    //2.总价，所有项的count*price累加
    const allCount = computed(() => cartList.value.reduce((a, c) => a + c.count, 0))
    const allPrice = computed(() => cartList.value.reduce((a, c) => a + c.count * c.price, 0))

    //单选功能
    const singleCheck = (skuId, selected) => {
        //思路：通过skuId找到对应的商品对象，然后修改它的selected属性
        const item = cartList.value.find((item) => skuId === item.skuId)
        if (item) {
            item.selected = selected
        }
    }

    //是否全选
    const isAll = computed(() => cartList.value.every((item) => item.selected))
    //全选功能
    const allCheck = (selected) => {
        cartList.value.forEach(
            (item) => item.selected = selected
        )

    }

    //清空购物车
    const clearCart =()=>{
        cartList.value=[]
    }

    //已选择数量
    const selectedCount = computed(() => cartList.value.reduce((a, c) => a + (c.selected ? c.count : 0), 0))
    //已选择商品价钱合计
    const selectedPrice = computed(() => cartList.value.reduce((a, c) => a + (c.selected ? c.count * c.price : 0), 0))


    return {
        cartList,
        addCart,
        delCart,
        allCount,
        allPrice,
        singleCheck,
        isAll,
        allCheck,
        selectedCount,
        selectedPrice,
        clearCart,
        updateNewList
    }
}, {
    persist: true
})